package com.cloud.sysmana.ser.impl;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.cloud.sysmana.entity.SysItem;
import com.cloud.sysmana.entity.SysRole;
import com.cloud.sysmana.entity.SysUser;
import com.cloud.sysmana.entity.dao.ISysItemDao;
import com.cloud.sysmana.entity.dao.ISysRoleDao;
import com.cloud.sysmana.ser.ISysItemService;
import com.cloud.util.base.RetType;
import com.cloud.util.other.StringUtil;
import com.cloud.util.page.PageInfo;
import com.cloud.util.page.ser.IPageService;



@Service
@Transactional
public class ItemService implements ISysItemService{
	
	@Resource(name="sysItemDao")
	private ISysItemDao sysItemDao;

	@Resource(name="pageService")
	private IPageService pageService;

	/**
	 * 获取系统岗位分页数据
	 * @param pageInfo
	 * @return
	 */
	@Override
	public PageInfo listRolePage(PageInfo pageInfo) {
		
		pageInfo = sysItemDao.listItemPage(pageInfo);
		pageInfo = pageService.getPageInfoList(pageInfo);
		
		return pageInfo;
	}

	@Override
	public RetType addOrUpdateItem(SysItem item) {
		//编辑岗位
				if(item.getId() > 0){
					System.out.println("update");
					updateItem(item);
				}else{//新增岗位
					System.out.println("add");
					addItem(item);
				}
				
				RetType reType = new RetType();
				reType.setErrCode(RetType.RET_SUCCESS);
				
				return reType;
	}
	
	/**
	 * 更新岗位
	 * @param role
	 */
	private void updateItem(SysItem role) {
		SysItem oldRole = sysItemDao.getItemById(role.getId());
		if(oldRole == null){
			return;
		}
		
		oldRole.setItemdescribe(role.getItemdescribe());
		oldRole.setItemname(role.getItemname());
		oldRole.setItemstatus(role.getItemstatus());
		oldRole.setProjectLeader(role.getProjectLeader());
		oldRole.setUrl(role.getUrl());
		
		sysItemDao.saveOrUpdateItem(oldRole);
	}
	
	
	/**
	 * 添加岗位
	 * @param role
	 */
	private void addItem(SysItem role) {
		
		
		sysItemDao.saveOrUpdateItem(role);
	}

	@Override
	public SysItem getItemById(int id) {
		// TODO Auto-generated method stub
		return sysItemDao.getItemById(id);
	}

	@Override
	public RetType delItem(String ids) {
String[] id = ids.split(",");
		
		for (int i = 0; i < id.length; i++) {
			int mid = StringUtil.StringToInt(id[i], 0);
			
			//通过用户id获取用户信息
			SysItem sysItem = sysItemDao.getItemById(mid);
			if(sysItem == null){
				continue;
			}
			
			
			
			//删除用户
			sysItemDao.delItem(sysItem);
		}
		
		RetType reType = new RetType();
		reType.setErrCode(RetType.RET_SUCCESS);
		
		return reType;
	}

}
